package com.peas.platform.module.icims.base.dict.domain.impl;

import cn.hutool.core.lang.Assert;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.dict.adapter.repository.DictValueRepository;
import com.peas.platform.module.icims.base.dict.adapter.repository.po.DictValuePo;
import com.peas.platform.module.icims.base.dict.domain.DictValueDomain;
import com.peas.platform.module.icims.base.dict.dto.DictTypeDto;
import com.peas.platform.module.icims.base.dict.dto.DictValueDto;
import com.peas.platform.module.icims.base.dict.service.DictTypeService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class DictValueDomainImpl extends BaseDomainImpl<DictValueDto, DictValuePo> implements DictValueDomain {

    @Resource
    private DictValueRepository dictValueRepository;
    @Resource
    private DictTypeService dictTypeService;

    @Override
    public BaseRepository<DictValuePo> getMapper() {
        return dictValueRepository;
    }

    @Override
    public Class<? extends BaseRepository<DictValuePo>> getRepositoryClass() {
        return DictValueRepository.class;
    }

    @Transactional
    @Override
    public int save(DictValueDto dto) {
        Assert.notNull(dto.getDictTypeId(), "请选择字典类型");
        DictTypeDto dtd = dictTypeService.getById(dto.getDictTypeId());
        Assert.notNull(dtd, "找不到字典类型");
        dto.setDictType(dtd.getDictType());
        return super.save(dto);
    }

    @Transactional
    @Override
    public int updateById(DictValueDto dto) {
        Assert.notNull(dto.getDictTypeId(), "请选择字典类型");
        DictTypeDto dtd = dictTypeService.getById(dto.getDictTypeId());
        Assert.notNull(dtd, "找不到字典类型");
        dto.setDictType(dtd.getDictType());
        return super.updateById(dto);
    }

}
